#include "xnlog.h"
#include "comstruct.h"

CXnLog::CXnLog(QObject *parent, int iProcess, QDateTime dtStart, const QString& strLogSrvName)
	: QObject(parent)
{
	m_iProcess = iProcess;
	m_dtStart = dtStart;
	m_strLogSrvName = strLogSrvName;
	m_bConnectedLogSrv = false;

	m_pTimer = new QTimer(this);
	m_pLocalSocket = new QLocalSocket;
	m_pLocalSocket->setReadBufferSize(10 * 1024);
	m_pBufferData = new char[1024];
	m_pCmdBufferData = new char[1024];
	m_iLogSelectType = RUN_LOG_STOP;

	QObject::connect(m_pLocalSocket, SIGNAL(disconnected()), this, SLOT(disconnected()));
	QObject::connect(this, SIGNAL(addLog(int, const QString&)), parent, SLOT(addLog(int, const QString&)));
	QObject::connect(m_pTimer, SIGNAL(timeout()), this, SLOT(connect2Srv()));	
}

CXnLog::~CXnLog()
{
	if (m_pTimer)
	{
		delete m_pTimer;
		m_pTimer = 0;
	}

	if(m_pLocalSocket)
	{
		delete m_pLocalSocket;
		m_pLocalSocket = 0;
	}

	if (m_pBufferData)
	{
		delete[] m_pBufferData;
		m_pBufferData = 0;
	}

	if (m_pCmdBufferData)
	{
		delete[] m_pCmdBufferData;
		m_pCmdBufferData = 0;
	}
}

void CXnLog::connect2Srv()
{
	if (m_bConnectedLogSrv) // Read Log Type
	{
		// Read Log Type
		memset(m_pCmdBufferData, 0, 1024);
		int iLen = m_pLocalSocket->read(m_pCmdBufferData, 1024);
		if (iLen >= 12) // MSG Type  iProcess   LogType
		{
			if (MSG_TYPE_LOG_SELECT == *((int*)m_pCmdBufferData))
			{
				if(PROCESS_ALL == *((int*)(m_pCmdBufferData + 4)) || m_iProcess == *((int*)(m_pCmdBufferData + 4)))
				{
					if (m_iLogSelectType != *((int*)(m_pCmdBufferData + 8)))
					{
						m_iLogSelectType = *((int*)(m_pCmdBufferData + 8));	

						emit addLog(RUN_LOG_RUN, "Selected Log Type Charged.");
					}					
				}
				else
				{
					m_iLogSelectType = RUN_LOG_STOP;
				}
			}			
		}
	}
	else
	{
		m_pLocalSocket->connectToServer(m_strLogSrvName);

		if (m_pLocalSocket->waitForConnected(1000))    // 连接成功
		{  		
			// Send register msg
			quint32* pUIMsg = 0;
			SLogRegister* pLogReg = 0;
			int iLen = 0;

			memset(m_pBufferData, 0, 1024);

			pUIMsg = (quint32*)m_pBufferData;
			pLogReg = (SLogRegister*)(m_pBufferData + 4);

			*pUIMsg = MSG_TYPE_LOG_REGISTER;
			pLogReg->iProcess = m_iProcess;
			pLogReg->iYear = m_dtStart.date().year();
			pLogReg->iMonth = m_dtStart.date().month();
			pLogReg->iDay = m_dtStart.date().day();
			pLogReg->iHour = m_dtStart.time().hour();
			pLogReg->iMin = m_dtStart.time().minute();
			pLogReg->iSec = m_dtStart.time().second();

			m_pLocalSocket->write(m_pBufferData, sizeof(SLogRegister) +  4);
			m_pLocalSocket->flush();

			// read
			if (m_pLocalSocket->waitForReadyRead(1500)) // timeout
			{
				memset(m_pBufferData, 0, 1024);
				iLen = m_pLocalSocket->read(m_pBufferData, 1024);
				if (iLen >= 8)
				{
					pUIMsg = (quint32*)m_pBufferData;

					if (MSG_TYPE_LOG_REGISTER == *pUIMsg)
					{
						m_bConnectedLogSrv = true;
						//	m_pTimer->stop();

						printf("Connect to XnLogSrv and Registe OK.\n");
						//emit addLog(RUN_LOG_OK, "Connect to XnLogSrv and Registe OK.");	
					}			
				}
			}		
			else
			{
				printf("Connect to XnLogSrv and Registe Timeout.\n");
			}
		}  
		else
		{
			//emit addLog(RUN_LOG_OK, "Try to Connect XnLogSrv FAILED.");	
			//printf("Try to Connect XnLogSrv FAILED.\n");
		}
	}	
}

void CXnLog::disconnected()
{
	m_bConnectedLogSrv = false;
	m_pTimer->start(1000);
}

void CXnLog::output2LogSrv(int iLogType, const QString& strLog)
{
	if (m_bConnectedLogSrv && !strLog.isEmpty())
	{
		int iLen = 0;
		memset(m_pBufferData, 0, 1024);

		*m_pBufferData = (char)iLogType;
		*(m_pBufferData + 1) = (char)m_iProcess;
		
		iLen = strlen(strLog.toLatin1().data());
		if(iLen > 1024 - 2 - 1)
		{
			iLen = 1024 - 2 - 1;
		}

		memcpy(m_pBufferData + 2, strLog.toLatin1().data(), iLen); 

		m_pLocalSocket->write(m_pBufferData, iLen + 2 + 1);
		m_pLocalSocket->flush();	
	}
}

void CXnLog::startRemoteLog()
{
	m_pTimer->start(1000);
}

int CXnLog::getLogSelectType()
{
	return m_iLogSelectType;
}
